package org.jeecg.modules.performance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.performance.Model.PerforMonthDetailModel;
import org.jeecg.modules.performance.entity.PerformanceMonth;
import org.jeecg.modules.performance.entity.PerformanceMonthDepart;
import org.jeecg.modules.performance.entity.PerformanceMonthDetail;
import org.jeecg.modules.performance.entity.PerformanceUserAdjust;
import org.jeecg.modules.performance.mapper.PerformanceMonthDetailMapper;
import org.jeecg.modules.performance.mapper.PerformanceMonthMapper;
import org.jeecg.modules.performance.mapper.PerformanceUserAdjustMapper;
import org.jeecg.modules.performance.service.IPerformanceMonthDepartService;
import org.jeecg.modules.performance.service.IPerformanceMonthDetailService;
import org.jeecg.modules.performance.service.IPerformanceUserAdjustService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.service.ISysDictItemService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: PerformanceMonthDetail
 * @Author: jeecg-boot
 * @Date:   2021-04-16
 * @Version: V1.0
 */
@Service
public class PerformanceMonthDetailServiceImpl extends ServiceImpl<PerformanceMonthDetailMapper, PerformanceMonthDetail> implements IPerformanceMonthDetailService {

    @Autowired
    private  PerformanceMonthDetailMapper performanceMonthDetailMapper;

    @Autowired

    private PerformanceMonthMapper performanceMonthMapper;
    @Autowired
    private ISysDictItemService sysDictItemService;
    @Autowired
    private IPerformanceMonthDepartService performanceMonthDepartService;

    @Autowired
    private IPerformanceUserAdjustService performanceUserAdjustService;
    @Autowired
    private PerformanceUserAdjustMapper performanceUserAdjustMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;


    @Override
    public Map<String, Object> getTotalforDetailMap(String perforMonthId,String departId) {
        Map<String, Object>  resultMap = new HashMap<>();
        Map<String, Object> perforData = performanceMonthDetailMapper.getPerforData(perforMonthId, null);
        PerformanceMonth performanceMonth = performanceMonthMapper.selectById(perforMonthId);
        QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_text","fix_salary");
        SysDictItem item = sysDictItemService.getOne(queryWrapper);
        Integer allfixedSalaryCount = 0;
        Integer fixedSalaryCount =0;
        allfixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,2,null);
        fixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,2,departId);
//        if(performanceMonth.getFixStatus()==2){
//          //  //固定薪资不参与考评
//          //  allfixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,2,null );
//
//            fixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,2,departId);
//        }else {
//
//
//        }
        QueryWrapper<PerformanceMonthDepart> queryWrapperDepart = new QueryWrapper<>();
        queryWrapperDepart.eq("depart_id",departId).eq("perfor_month_id",perforMonthId);
        PerformanceMonthDepart one = performanceMonthDepartService.getOne(queryWrapperDepart);
        Map<String, Object> departData = performanceMonthDetailMapper.getPerforData(perforMonthId, departId);

        perforData.put("peopleOut",allfixedSalaryCount);
        departData.put("peopleOut",fixedSalaryCount);
        if(one != null){
            departData.put("sureStatus",one.getSureFlag());

        }else {
            departData.put("sureStatus",1);

        }

        QueryWrapper<PerformanceMonthDepart> queryWrapperDe = new QueryWrapper<>();
        queryWrapperDe.eq("perfor_month_id",perforMonthId);
        List<PerformanceMonthDepart> list = performanceMonthDepartService.list(queryWrapperDe);
         if(CollectionUtils.isEmpty(list)){
             perforData.put("canSure",false);

         }else {
             Integer canSure = 0;
             for (PerformanceMonthDepart performanceMonthDepart : list) {
                 if(performanceMonthDepart.getSureFlag().equals(2)){
                     canSure++;
                 }

             }
             if(canSure == list.size() ){
                 perforData.put("canSure",true);

             }else {
                 perforData.put("canSure",false);

             }
         }

      //  List<PerformanceUserAdjust> monthList = performanceUserAdjustMapper.getMonthList(perforMonthId);
         Float totalAdjust = getTotalAdjust( perforMonthId,null,1);



        perforData.put("adjustAmount",totalAdjust);
        resultMap.put("all",perforData);

        resultMap.put("depart",departData);
        resultMap.put("performanceMonth",performanceMonth);

        return resultMap;
    }

    @Override
    public Map<String, Object> getTotalforDepartDetailMap(String perforMonthId,String departId) {
        Map<String, Object>  resultMap = new HashMap<>();

        QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_text","fix_salary");
        SysDictItem item = sysDictItemService.getOne(queryWrapper);
        Integer fixedSalaryCount =0;
        if(item.getItemValue().equals("2")){

            fixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,2,departId);
        }else {

            fixedSalaryCount = performanceMonthDetailMapper.getCheckCount(perforMonthId,1,departId);
        }
        QueryWrapper<PerformanceMonthDepart> queryWrapperDepart = new QueryWrapper<>();
        QueryWrapper<SysDepart>departQueryWrapper = new QueryWrapper<>();

        departQueryWrapper.eq("id",departId);

        SysDepart sysDepart = sysDepartMapper.selectOne(departQueryWrapper);


        queryWrapperDepart.eq("depart_id",sysDepart.getFirstId()).eq("perfor_month_id",perforMonthId);


        PerformanceMonthDepart one = performanceMonthDepartService.getOne(queryWrapperDepart);
        Map<String, Object> departData = performanceMonthDetailMapper.getPerforData(perforMonthId, departId);
        departData.put("peopleOut",fixedSalaryCount);
        if(one != null){
            departData.put("sureStatus",one.getSureFlag());
            departData.put("id",one.getId());

        }else {
            departData.put("sureStatus",1);

        }
        Float totalAdjust = getTotalAdjust( perforMonthId,departId,2);



        departData.put("adjustAmount",totalAdjust);




        resultMap.put("depart",departData);

        return resultMap;
    }


    @Override
    public Float getTotalAdjust(String perforMonthId, String departId, Integer type){
        Float totalAdjust =0f;
        List<PerformanceUserAdjust> monthList = performanceUserAdjustMapper.getMonthList(perforMonthId,departId,type);

        if(!CollectionUtils.isEmpty(monthList)){
            for (PerformanceUserAdjust performanceUserAdjust : monthList) {
                if(performanceUserAdjust.getAdjustType().equals(1)){
                    //加 显示是减
                    totalAdjust -= performanceUserAdjust.getAdjustAmount();
                }else {
                    totalAdjust += performanceUserAdjust.getAdjustAmount();

                }
            }
        }
        return  totalAdjust;
    }

    @Override
    public Page<PerforMonthDetailModel> getPerforMonthDetailList(Page<PerforMonthDetailModel> page, String perforMonthId,String departId) {
        List<PerforMonthDetailModel> perforMonthDetailList = performanceMonthDetailMapper.getPerforMonthDetailList(page, perforMonthId,departId);


        for (PerforMonthDetailModel perforMonthDetailModel : perforMonthDetailList) {

                //固定工资不参与考评
                if(perforMonthDetailModel.getSalaryAmount() >0.0f){
                    perforMonthDetailModel.setSalaryTypeName("固定薪资");

                }else {
                    perforMonthDetailModel.setSalaryTypeName("绩效薪资");

                }


                QueryWrapper<PerformanceUserAdjust> queryWrapperUser = new QueryWrapper<>();
                queryWrapperUser.eq("detail_id",perforMonthDetailModel.getId());
                List<PerformanceUserAdjust> list = performanceUserAdjustService.list(queryWrapperUser);
                Float adminAdjust = 0f;
                Float departAdjust = 0f;
                if(!CollectionUtils.isEmpty(list)){

                   for (PerformanceUserAdjust performanceUserAdjust : list) {
                       if(performanceUserAdjust.getDetailId().equals(perforMonthDetailModel.getId()) ){
                            if(performanceUserAdjust.getOpType().equals(1)){
                                //行政调整
                                if(performanceUserAdjust.getAdjustType().equals(1)){
                                    adminAdjust += performanceUserAdjust.getAdjustAmount();
                                }else {
                                    adminAdjust -= performanceUserAdjust.getAdjustAmount();
                                }
                            }else {
                                if(performanceUserAdjust.getAdjustType().equals(1)){
                                    departAdjust +=performanceUserAdjust.getAdjustAmount();
                                }else {
                                    departAdjust -= performanceUserAdjust.getAdjustAmount();
                                }
                            }
                       }

                   }

                }
                perforMonthDetailModel.setBaseAmount(perforMonthDetailModel.getPostAmount()+perforMonthDetailModel.getSalaryAmount());
                perforMonthDetailModel.setAdminAdjust(adminAdjust);
                perforMonthDetailModel.setDepartAdjust(departAdjust);
            perforMonthDetailModel.setAdjustAmount(perforMonthDetailModel.getAdminAdjust()+perforMonthDetailModel.getDepartAdjust());

            perforMonthDetailModel.setSalary(perforMonthDetailModel.getSalaryAmount()+perforMonthDetailModel.getPostAmount()+perforMonthDetailModel.getScoreAmount()+adminAdjust+departAdjust);



        }
        return page.setRecords(perforMonthDetailList);

    }

    @Override
    public Page<PerforMonthDetailModel> getPerforMonthPersonalDetailList(Page<PerforMonthDetailModel> page, String userId,String name,Integer sureFlag) {
        List<PerforMonthDetailModel> perforMonthDetailList = performanceMonthDetailMapper.getPerforPersonalDetailList(page, userId,name,sureFlag);

        QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_text","fix_salary");
        SysDictItem item = sysDictItemService.getOne(queryWrapper);


        for (PerforMonthDetailModel perforMonthDetailModel : perforMonthDetailList) {

                //固定工资不参与考评
                if(perforMonthDetailModel.getSalaryAmount() >0){
                    perforMonthDetailModel.setSalaryTypeName("固定薪资");

//                    if(item.getItemValue().equals("2")){
//
//                    }else {
//                        perforMonthDetailModel.setSalaryTypeName("绩效薪资");
//
//                    }

                }else {
                    perforMonthDetailModel.setSalaryTypeName("绩效薪资");

                }

                QueryWrapper<PerformanceUserAdjust> queryWrapperUser = new QueryWrapper<>();
                queryWrapperUser.eq("detail_id",perforMonthDetailModel.getId());
                List<PerformanceUserAdjust> list = performanceUserAdjustService.list(queryWrapperUser);
                Float adminAdjust = 0f;
                Float departAdjust = 0f;
                if(!CollectionUtils.isEmpty(list)){

                    for (PerformanceUserAdjust performanceUserAdjust : list) {
                        if(performanceUserAdjust.getDetailId().equals(perforMonthDetailModel.getId()) ){
                            if(performanceUserAdjust.getOpType().equals(1)){
                                //行政调整
                                if(performanceUserAdjust.getAdjustType().equals(1)){
                                    adminAdjust += performanceUserAdjust.getAdjustAmount();
                                }else {
                                    adminAdjust -= performanceUserAdjust.getAdjustAmount();
                                }
                            }else {
                                if(performanceUserAdjust.getAdjustType().equals(1)){
                                    departAdjust +=performanceUserAdjust.getAdjustAmount();
                                }else {
                                    departAdjust -= performanceUserAdjust.getAdjustAmount();
                                }
                            }
                        }

                    }

                }
                perforMonthDetailModel.setBaseAmount(perforMonthDetailModel.getPostAmount()+perforMonthDetailModel.getSalaryAmount());
                perforMonthDetailModel.setAdminAdjust(adminAdjust);
                perforMonthDetailModel.setDepartAdjust(departAdjust);
                perforMonthDetailModel.setSalary(perforMonthDetailModel.getScoreAmount()+perforMonthDetailModel.getPostAmount()+perforMonthDetailModel.getSalaryAmount()+adminAdjust+departAdjust);


        }
        return page.setRecords(perforMonthDetailList);

    }



    public  void   fenpeiPerfor(String perforMonthId,Float perforAmount,Float departAmount,Float fenpeiAmount){

//        PerformanceMonth performanceMonth = new PerformanceMonth();
//        performanceMonth.setId(perforMonthId);
//        performanceMonth.setDepartAmount(departAmount);
//        performanceMonth.setPerforAmount(perforAmount);
//        performanceMonth.setFenpeiAmount(fenpeiAmount);
//        performanceMonthMapper.updateById(performanceMonth);
//
//        Page<PerforMonthDetailModel> page = new Page<PerforMonthDetailModel>(1, Integer.MAX_VALUE);
//
//        Page<PerforMonthDetailModel> list = getPerforMonthDetailList(page, perforMonthId);
//        Float allScore= 0f;
//        for (PerforMonthDetailModel perforMonthDetailModel : list.getRecords()) {
//                allScore += perforMonthDetailModel.getSpreadScore()+perforMonthDetailModel.getOtherScore()+ perforMonthDetailModel.getTimeScore();
//
//        }
//        if(!CollectionUtils.isEmpty( list.getRecords())) {
//            if(allScore>0){
//           // List<PerformanceMonthDetail> performanceMonthDetailList = new ArrayList<>();
//
//            for (PerforMonthDetailModel perforMonthDetailModel : list.getRecords()) {
//                perforMonthDetailModel.setAllAmount(perforAmount);
//
//                PerformanceMonthDetail performanceMonthDetail = new PerformanceMonthDetail();
//                if (allScore == 0) {
//                    perforMonthDetailModel.setScoreAmount(0f);
//
//                } else {
//                    Float amount = (perforAmount / allScore) * (perforMonthDetailModel.getSpreadScore() + perforMonthDetailModel.getOtherScore() + perforMonthDetailModel.getTimeScore());
//                    perforMonthDetailModel.setScoreAmount(amount);
//
//
//                }
//
//                BeanUtils.copyProperties( perforMonthDetailModel,performanceMonthDetail);
//
//               // performanceMonthDetailList.add(performanceMonthDetail);
//                performanceMonthDetailMapper.updateById(performanceMonthDetail);
//             }
//            }
//        }

    }
}
